﻿#region License
// Copyright 2010 Buu Nguyen (http://www.buunguyen.net/blog)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://www.codeplex.com/aspmvcvalidation
#endregion

using System;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace Mvc.Validation
{
    /// <summary>
    /// Represents a validation exception which contains an object of 
    /// type <see cref="ValidationResults"/>.
    /// </summary>
    public class EntityValidationException : Exception
    {
        /// <summary>
        /// The enterprise library's <code>ValidationResults</code>.
        /// </summary>
        public ValidationResults Errors { get; set; }

        /// <summary>
        /// An exception representing a list of validation errors.
        /// </summary>
        /// <param name="errors">The enterprise library's <code>ValidationResults</code></param>
        public EntityValidationException(ValidationResults errors)
        {
            Errors = errors;
        }

        /// <summary>
        /// An exception not associated with any specific property.
        /// </summary>
        /// <param name="error">The validation error.</param>
        public EntityValidationException(string error)
        {
            var errors = new ValidationResults();
            errors.AddResult(new ValidationResult(error, null, string.Empty, null, null));
            Errors = errors;
        }

        /// <summary>
        /// An exception associated with a specific <paramref name="key"/> (property).
        /// </summary>
        /// <param name="key">The property this exception is associated with.</param>
        /// <param name="error">The validation error.</param>
        public EntityValidationException(string key, string error)
        {
            var errors = new ValidationResults();
            errors.AddResult(new ValidationResult(error, null, key, null, null));
            Errors = errors;
        }
    }
}
